package com.sh.mytool.cv.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sh.common.extjs.CUDResult;
import com.sh.common.properties.PropertiesUtils;
import com.sh.common.web.ResponseHandle;
import com.sh.mytool.cv.domain.User;

@Controller
public class UserController {

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public void userLogin(User user, HttpSession session, HttpServletResponse response, PrintWriter out) throws Exception {
        String name = PropertiesUtils.getValueByResourceBundle("project", "admin.name");
        String pwd = PropertiesUtils.getValueByResourceBundle("project", "admin.pwd");
        // TODO
        Boolean result = false;
        if(user.getUsername().equals(name) && user.getPassword().equals(pwd)){
            result = true;
            session.setAttribute("login", "login");
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(result), true);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/user/logout", method = RequestMethod.POST)
    public void userLogout(HttpSession session, HttpServletResponse response, PrintWriter out) throws Exception {
         session.removeAttribute("login");
         String json = ResponseHandle.handleJson("", "", new CUDResult(true), true);
         response.setCharacterEncoding("UTF-8");
         out.write(json);
         out.flush();
         out.close();
    }
}
